#!/bin/sh

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"
export APPDIR="$APPDIR"

# preferences
. $APPDIR/preferences
[ -f "$HOME/.config/cuesplitter/preferences" ] && . "$HOME/.config/cuesplitter/preferences"

# plugins
PLUGINS="<item>$DEFAULT_PLUGIN</item>"
for PL in `ls $APPDIR/plugins | grep -v $DEFAULT_PLUGIN`
do
 PLUGINS="$PLUGINS<item>$PL</item>"
done

case $1 in
 -options)
 export OPTIONS_DLG="
 <window title=\"$LOC_OPTIONS_TITLE\" icon_name=\"gtk-preferences\">
  <vbox>
   <notebook labels=\"$LOC_DIR_OUT\">
    <vbox>
     <frame $LOC_OUTDIR>
      <hbox>
       <entry accept=\"directory\">
        <default>$OUTDIR</default>
        <variable>OUTDIR</variable>
       </entry>
       <button>
        <input file stock=\"gtk-open\"></input>
        <action type=\"fileselect\">OUTDIR</action>
       </button>
      </hbox>
      <checkbox>
       <label>$LOC_ARTIST_DIR</label>
       <variable>ARTIST_DIR</variable>
       <default>$ARTIST_DIR</default>
      </checkbox>
      <checkbox>
       <label>$LOC_ALBUM_DIR</label>
       <variable>ALBUM_DIR</variable>
       <default>$ALBUM_DIR</default>
      </checkbox>
      <checkbox>
       <label>$LOC_INC_DATE</label>
       <variable>INCLUDE_DATE</variable>
       <default>$INCLUDE_DATE</default>
      </checkbox>
      <checkbox>
       <label>$LOC_GENPLAY</label>
       <variable>CREATE_PLAYLIST</variable>
       <default>$CREATE_PLAYLIST</default>
      </checkbox>
     </frame>
     <frame $LOC_TEMPDIR>
      <hbox>
       <entry accept=\"directory\">
        <default>$TEMP_DIR</default>
        <variable>TEMP_DIR</variable>
       </entry>
       <button>
        <input file stock=\"gtk-open\"></input>
        <action type=\"fileselect\">TEMP_DIR</action>
       </button>
      </hbox>
     </frame>
    </vbox>
    <vbox>
     <frame $LOC_NAMING>
      <hbox>
       <text>
        <label>$LOC_NAME_FORMAT</label>
       </text>
       <entry accept=\"file\">
        <default>\"$TITLE_FORMAT\"</default>
        <variable>TITLE_FORMAT</variable>
       </entry>
      </hbox>
      <text use_markup=\"true\">
       <label>\"<b>$LOC_LEGEND</b>\"</label>
      </text>
      <hbox>
       <text>
        <label>\"%n - $LOC_TRACK_NUM\"</label>
       </text>
      </hbox>
      <hbox>
       <text>
        <label>\"%p - $LOC_ARTIST\"</label>
       </text>
      </hbox>
      <hbox>
       <text>
        <label>\"%a - $LOC_ALBUM\"</label>
       </text>
      </hbox>
      <hbox>
       <text>
        <label>\"%t - $LOC_TRACK_TITLE\"</label>
       </text>
      </hbox>
     </frame>
     <frame $LOC_DEFAULT_PLUGIN>
      <hbox>
       <combobox>
        <variable>DEFAULT_PLUGIN</variable>
        $PLUGINS
       </combobox>
       <button>
        <input file stock=\"gtk-preferences\"></input>
        <label>$LOC_PLUGIN_OPTS</label>
        <action>. $APPDIR/plugins/\$DEFAULT_PLUGIN/plugin.sh && plugin_options</action>
       </button>
      </hbox>
     </frame>
    </vbox>
   </notebook>
   <hbox>
    <button>
     <input file stock=\"gtk-close\"></input>
     <label>$LOC_CLOSE</label>
    </button>
   </hbox>
  </vbox>
 </window>"
 gtkdialog3 --program OPTIONS_DLG > "$HOME/.config/cuesplitter/preferences"
 ;;
 -about)
 export ABOUT_DLG="
 <window title=\"$LOC_ABOUT_TITLE - $VERSION\">
  <vbox>
   <text use_markup=\"true\">
    <label>\"<b>$LOC_AUDIO_UTIL</b>\"</label>
   </text>
   <frame About>
    <text>
     <label>\"$LOC_ABOUT_TEXT\"</label>
    </text>
   </frame>
   <hbox homogeneous=\"true\">
    <button>
     <input file stock=\"gtk-close\"></input>
     <label>$LOC_CLOSE</label>
    </button>
   </hbox>
  </vbox>
 </window>"
 gtkdialog3 --program ABOUT_DLG
 ;;
 -help)
 gxmessage "help dialog"
 ;;
 -split)
 # load the output plugin
 . $APPDIR/plugins/$PLUGIN/plugin.sh
 
 # make sure we have valid cue and bin files, and a destination
 [ "$CUEFILE" = "" ] && gxmessage "No cue file chosen" && exit 1
 [ "$BINFILE" = "" ] && gxmessage "No bin file chosen" && exit 1
 [ "$OUTDIR" = "" ] && gxmessage "No output directory chosen" && exit 1
 
 export ALBUM="`cat "$CUEFILE" | grep TITLE | cut -f 2 -d '"' | head -n 1`"  #'
 export DATE="`cat "$CUEFILE" | grep DATE | cut -f 3 -d ' '`"
 export GENRE="`cat "$CUEFILE" | grep GENRE | cut -f 3 -d ' '`"
 export ARTIST="`cat "$CUEFILE" | grep PERFORMER | head -n 1 | cut -f 2 -d '"'`"  #'
 export COMMENT="`cat "$CUEFILE" | grep COMMENT | cut -f 2 -d '"'`"  #'
 
 cd "$OUTDIR"
 
 # create subdirectories
 if [ "$ARTIST_DIR" = "true" ];then
  mkdir "$ARTIST" 2>/dev/null
  cd "$ARTIST"
 fi
 if [ "$ALBUM_DIR" = "true" ];then
  mkdir "$ALBUM" 2>/dev/null
  cd "$ALBUM"
  OUTPUT_DIR="`pwd`"
 fi
 
 # move to our tem directory
 cd "$TEMP_DIR"
 
 # Split the bin file into separate wavs using shntool
 shntool split -f "$CUEFILE" -t "%n - %t" -o wav "$BINFILE"
 for TRACK in `ls *.wav | grep "[0-9] -" | tr ' ' '%'`
 do
  export FNAME="`echo $TRACK | tr '%' ' '`"
  export TITLE="`echo $TRACK | tr '%' ' ' | cut -b 5-99 | sed s*.wav**`"
  export TRACKNUM="`echo $TRACK | cut -b 1-2`"
  convert_file
  # rename all files
  FILENAME="`echo $TITLE_FORMAT | sed "s/%n/$TRACKNUM/g" | sed "s/%p/$ARTIST/g" | sed "s/%a/$ALBUM/g" | sed "s/%t/$TITLE/g"`"
  mv -f "`basename "$FNAME" .wav`.$EXT" "$OUTPUT_DIR/$FILENAME".$EXT
  rm "$FNAME"
  [ "$CREATE_PLAYLIST" = "true" ] && echo "$FILENAME".$EXT >> "$OUTPUT_DIR/$ARTIST - $ALBUM".m3u
 done
 cd "$CURDIR"
 ;;
esac
